package com.sinosoft.dsp.platform.components.portal.iteration;

import java.util.List;
import java.util.Map;

import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

import com.sinosoft.dsp.platform.exception.SinoPlatformException;
import com.sinosoft.dsp.system.core.log.Log;

/**
 * Sino懒加载数据模型
 * 
 * @author 潘巍（PeterPan）
 * @since 2011-5-31 下午05:47:25
 * 
 * @param <T>
 */
public class SinoLazyDataModel<T extends IDataTableObject> extends
		LazyDataModel<T> {

	private static final long serialVersionUID = -1768489337033502629L;

	private static final Log log = Log
			.getLog(SinoLazyDataModel.class.getName());

	private ISinoLazyDataTableViewBean<T> vb;

	private String sortField;

	private SortOrder sortOrder;

	private boolean initCount;

	Map<String, String> filters;

	public SinoLazyDataModel(ISinoLazyDataTableViewBean<T> vb) {
		this.vb = vb;
	}

	@Override  
    public T getRowData(String rowKey) {  
        
        return null;  
    }  
  
    @Override  
    public Object getRowKey(T t) {  
        return null;  
    } 
    
	@Override
	public List<T> load(int first, int pageSize, String sortField,
			SortOrder sortOrder, Map<String, String> filters) {

		try {
			loadRowCount(sortField, sortOrder, filters);
			return this.vb.fetchPage(first, pageSize, sortField, sortOrder,
					filters);
		} catch (SinoPlatformException e) {
			log.error(e.getMessage());
		}
		return null;
	}

	private void loadRowCount(String sortField, SortOrder sortOrder,
			Map<String, String> filters) throws SinoPlatformException {
		boolean reload = false;

		if (!compareStrings(this.sortField, sortField)) {
			this.sortField = sortField;
			reload = true;
		}

		if (reload) {
			this.sortOrder = sortOrder;
		} else {
			if (this.sortOrder != sortOrder) {
				this.sortOrder = sortOrder;
				reload = true;
			}
		}

		if (reload) {
			this.filters = filters;
		} else {
			if (this.filters == null && filters != null || this.filters != null
					&& filters == null) {
				this.filters = filters;
				reload = true;
			} else if (this.filters != null && filters != null) {
				for (String key : this.filters.keySet()) {
					if (!compareStrings(this.filters.get(key), filters.get(key))) {
						this.filters = filters;
						reload = true;
						break;
					}
				}
				if (!reload) {
					for (String key : filters.keySet()) {
						if (!compareStrings(this.filters.get(key), filters
								.get(key))) {
							this.filters = filters;
							reload = true;
							break;
						}
					}
				}
			}
		}

		if (reload || !this.initCount) {
			this.setRowCount(this.vb.fetchRowCount(sortField, sortOrder,
					filters));
			this.initCount = true;
		}
	}

	private boolean compareStrings(String str1, String str2) {
		if (this.sortField != null && !this.sortField.equals(sortField)) {
			return false;
		} else if (sortField != null && !sortField.equals(this.sortField)) {
			return false;
		}
		return true;
	}

}
